package com.nju.crawler.config.source;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据源配置
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        //实体管理
        entityManagerFactoryRef="entityManagerFactoryFirstly",
        //事务管理
        transactionManagerRef="transactionManagerFirstly",
        //实体扫描,设置Repository所在位置
        basePackages= { "com.nju.crawler.repository.jpa" })
public class CCLabConfig {

    /**
     * springboot有默认的配置，但是在 application.yml中覆盖了springboot的默认配置
     */
    @Autowired
    @Qualifier("primaryDataSource")
    private DataSource appDataSource;

    @Autowired
    private Environment env;

    @Autowired
    private HibernateProperties hibernateProperties;

    @Autowired
    private JpaProperties jpaProperties;

    @Bean(name = "entityManagerFirstly")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryFirstly(builder)
                .getObject()
                .createEntityManager();
    }

    @Bean(name = "entityManagerFactoryFirstly")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryFirstly (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(appDataSource)
                //.properties(getVendorProperties())
                .packages("com.nju.crawler.domain")
                .persistenceUnit("firstlyPersistenceUnit")
                .build();
    }

//    private Map<String, String> getVendorProperties() {
//        Map<String, String> jpaProperties = new HashMap<>(16);
//        jpaProperties.put("hibernate.hbm2ddl.auto", "update");
//        jpaProperties.put("hibernate.show_sql", env.getProperty("spring.jpa.show-sql"));
//        jpaProperties.put("hibernate.dialect", env.getProperty("spring.jpa.database-platform"));
//        jpaProperties.put("hibernate.format_sql", env.getProperty("spring.jpa.hibernate.format_sql"));
//        jpaProperties.put("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext");
//
//        return jpaProperties;
//    }

    @Bean(name = "transactionManagerFirstly")
    PlatformTransactionManager transactionManagerFirstly(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryFirstly(builder).getObject());
    }
}
